package com.example.no1admin.service.impl;

import com.example.no1admin.ex.ServiceException;
import com.example.no1admin.mapper.AdminMapper;
import com.example.no1admin.pojo.vo.adminLogin.AdminListItemVO;
import com.example.no1admin.pojo.vo.adminLogin.AdminSimpleVO;
import com.example.no1admin.service.IAdminService;
import com.example.no1admin.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;


    public AdminServiceImpl() {
        log.debug("创建业务逻辑对象：AdminServiceImpl");
    }


    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除管理员的业务，参数：{}", id);

        // 检查尝试删除的管理员数据是否存在
        AdminSimpleVO queryResult = adminMapper.getSimpleById(id);
        if (queryResult == null) {
            String message = "删除管理员失败，尝试删除的数据不存在！";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 执行删除
        int rows = adminMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除管理员失败，服务器忙，请稍后再次尝试！";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
    }


    @Override
    public List<AdminListItemVO> list() {
        log.debug("开始处理查询管理员列表的业务");
        return adminMapper.list();
    }
}
